package SI::Person::BaseInfo;

use base qw(MyDB::Object);

__PACKAGE__->meta->setup(
    table => 'BaseInfo',
    columns =>
    [
        sn => { type => 'varchar', length => 16, not_null => 1 },
        name => { type => 'varchar', length => 40, not_null => 1 },
        id => { type => 'varchar', length => 18 },
        sex => { type => 'varchar', length => 10, not_null => 1 },
        nation_code => { type => 'varchar', length => 3, not_null => 1},
        company_id => { type => 'varchar', length => 5, not_null => 1},
        tel => { type => 'varchar', length => 40 },
        address => { type => 'varchar', length => 200 },
        postcode => { type => 'varchar', length => 6 },
        status_code => { type => 'varchar', length => 2 },
    ],
    allow_inline_column_values => 1,
    primary_key_columns => [ 'sn' ],
    foreign_keys =>
    [
        nation =>
        {
            class => 'Nation',
            key_columns => { nation_code => 'code'},
        },

        company =>
        {
            class => 'Company',
            key_columns => { company_id => 'id'},
        },

        status =>
        {
            class => 'PersonStatus',
            key_columns => { status_code => 'code'},
        },
    ],

);

__PACKAGE__->meta->make_manager_class('baseinfos');

sub init_tab {
    my $self = shift;
    my $stmt = 'CREATE TABLE IF NOT EXISTS BaseInfo (
    sn VARCHAR(16) PRIMARY KEY NOT NULL, /* 序列号   */
    name VARCHAR(40) NOT NULL,           /* 姓名     */
    id VARCHAR(18) NOT NULL,             /* 身份证号 */
    sex VARCHAR(6) NOT NULL,             /* 性别     */
    nation_code VARCHAR(3) NOT NULL,     /* 民族代码 */
    company_id VARCHAR(5) NOT NULL,      /* 单位编码 */
    tel VARCHAR(30) ,                    /* 电话号码 */
    address VARCHAR(200) ,               /* 通讯地址 */
    postcode VARCHAR(6) ,                /* 邮政编码 */
    status_code VARCHAR(2) NOT NULL,      /* 状态码   */

    FOREIGN KEY (company_id) REFERENCES ComInfo(id) ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY (nation_code) REFERENCES Nation(code) ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY (status_code) REFERENCES PersonStatus(code) ON DELETE CASCADE ON UPDATE CASCADE
    );';
    my $db = $self->init_db;
    my $dbh = $db->dbh or die $db->error;
    $db->do_transaction(sub {$dbh->do($stmt)})
        or die $db->error;
    return 1;
}










1;
